package com.languohui.lgh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.languohui.lgh.client.RedisClient;
import com.languohui.lgh.domain.ProjectType;
import com.languohui.lgh.mapper.ProjectTypeMapper;
import com.languohui.lgh.service.IProjectTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2021-12-28
 */
@Service
public class ProjectTypeServiceImpl extends ServiceImpl<ProjectTypeMapper, ProjectType> implements IProjectTypeService {

    @Autowired
    private RedisClient redisClient;

    private final String PROJECT_TYPE="lgh:project_type:all";
    @Override
    public List<ProjectType> loadTreeData() {
        List<ProjectType> list = null;
        String projectType = redisClient.getValue(PROJECT_TYPE);
        //如果redis中没有数据，使用双重校验第一次查询数据库
        if(StringUtils.isEmpty(projectType)){
            //查询数据库，防止缓存穿透，同步代码块
            synchronized (ProjectTypeServiceImpl.class){
                projectType = redisClient.getValue(PROJECT_TYPE);
                if(StringUtils.isEmpty(projectType)){
                    list = loadTreeDataLoop();
                    //list转string
                    String JSONStr = JSONObject.toJSONString(list);
                    //放入redis
                    redisClient.setValue(PROJECT_TYPE,JSONStr);
                    return list;
                }
            }
        }
        //从redis中取数据
        list = JSONObject.parseArray(projectType,ProjectType.class);
        return list;
    }

    public List<ProjectType> loadTreeDataLoop(){
        //初始化一个集合放一级
        List<ProjectType> firstTypes = new ArrayList<>();
        //查询所有类型
        List<ProjectType> projecttypes = baseMapper.selectList(null);
        //创建一个Map,将ProjectType数据存到map中，key使用id，value就是ProjectType
        Map<Integer,ProjectType> map = new HashMap<>();
        for (ProjectType projecttype : projecttypes) {
            map.put(projecttype.getId(),projecttype);
        }
        //循环projecttypes，分配一级类型和非一级类型
        for (ProjectType projecttype : projecttypes) {
            if(projecttype.getPid().intValue() == 0){
                firstTypes.add(projecttype);
            }else{
                ProjectType parent = map.get(projecttype.getPid());
                if(parent != null){
                    parent.getChildren().add(projecttype);
                }
            }
        }
        return firstTypes;
    }

    /**
     * 增删改同步操作redis
     */
    private void synOperate() {
        List<ProjectType> list = loadTreeDataLoop();
        String JSONStr = JSONObject.toJSONString(list);
        redisClient.setValue(PROJECT_TYPE,JSONStr);
    }

    @Override
    public boolean save(ProjectType entity) {
        super.save(entity);
        synOperate();
        return true;
    }

    @Override
    public boolean removeById(Serializable id) {
        super.removeById(id);
        synOperate();
        return true;
    }


    @Override
    public boolean updateById(ProjectType entity) {
        super.updateById(entity);
        synOperate();
        return true;
    }
}
